---
title: "Talking Politics in a Polarized America (Political Behavior)"
subtitle: "Replication Materials for Experimental Findings"
author: "Amber Lee & Chloe Ahn"
date: "`r Sys.Date()`"
output: html_document
---

**Required Libraries**

```{r setup, include = F}
knitr::opts_chunk$set(echo=TRUE)

library('pacman')
pacman::p_load(
  tidyverse, dplyr, ggplot2, scales, 
  stargazer, psych, plotrix, fixest)
```

**Data**

```{r include=FALSE}
load("Experiment.RData")
```

**Analysis**

*Manipulation Check*

```{r echo=FALSE}
dat <- dat %>%
  mutate(
    percpol_3r = 8 - percpol_3,
    manicheck = rowMeans(
      across(c(percpol_1, percpol_2, percpol_3r, percpol_4, percpol_5)),
      na.rm = TRUE)
  )

t.test(manicheck ~ condition, dat)
```

*Covariates*

```{r include=FALSE}
dat <- dat %>%
  mutate(
    shyness_1r = 6 - shyness_1,
    social     = rowMeans(across(c(shyness_1r, shyness_2)), na.rm = TRUE),
    socialH    = if_else(social <= 3.5, 0L, 1L),
    conflictH  = if_else(conflict <= 4, 0L, 1L)
  )

extreme <- function(x){abs(3 - x)}

dat <- dat %>% 
  mutate_at(vars(attitude_1:attitude_4), extreme) %>% 
  mutate(attextreme = coalesce(attitude_1,attitude_2,attitude_3,attitude_4))

dat <- dat %>% 
  mutate(
    strongp = case_when(rep==1|dem==1 ~ 1, TRUE ~ 0),
    netstrong = network_1,
    netweak = rowMeans(select(., network_2,network_3))
    )

range01 <- function(x,...){(x-min(x,...))/(max(x,...)-min(x,...))}

dat <- dat %>% 
  mutate_at(., c("attextreme","netstrong","netweak","polinterest"), range01)

dat %>%
  select(conflictH,socialH,polinterest,attextreme,strongp,netstrong,netweak) %>%
  describe()

setFixest_fml(..ctrl= ~+ conflictH + socialH + polinterest + attextreme + strongp + netstrong + netweak)
```

*Dependent Variables*

```{r echo=FALSE}
reverse <- function(x,...){6-x}
dat <- dat %>% 
  mutate_at(., c("freetospeak_1","freetospeak_2","freetospeak_3","freetospeak_4"),
            reverse)

dat <- dat %>%
  mutate(
  sc_st = freetospeak_1, 
  sc_wt = rowMeans(across(c(freetospeak_2, freetospeak_3, freetospeak_4)),
                   na.rm = TRUE),
  sc_pol = if_else(showfirst == "political", dinnertopic1, dinnertopic2),
  sc_nonpol = if_else(showfirst == "political", dinnertopic2, dinnertopic1)
  ) %>%
  mutate_at(.,c("sc_pol","sc_nonpol"), reverse) %>%
  mutate_at(c("sc_st","sc_wt","sc_pol","sc_nonpol"), range01)

dat %>% select(starts_with("sc_"),email,minamount) %>% describe()
```

**Figures**

*Figure 1*

```{r echo=FALSE}
sc_st.m <- lm(xpd(sc_st ~ condition + ..ctrl), dat)
sc_wt.m <- lm(xpd(sc_wt ~ condition + ..ctrl), dat)
sc_pol.m <- lm(xpd(sc_pol ~ condition + as.factor(poltopic) + ..ctrl), dat)
sc_nonpol.m <- lm(xpd(sc_nonpol ~ condition + as.factor(nonpoltopic) + ..ctrl), dat)

stargazer(sc_st.m, sc_wt.m, sc_pol.m, sc_nonpol.m, type="text", report="vcsp", 
          dep.var.labels=c("strongties","weakties","poltopics","nonpol topics"),
          omit=c("poltopic","nonpoltopic"))

# one-tailed p-values
output <- summary(sc_wt.m); t <- coef(output)[ ,3]; df <- output$df 
pt(t, df, lower = FALSE)[2]
output <- summary(sc_pol.m); t <- coef(output)[ ,3]; df <- output$df 
pt(t, df, lower = FALSE)[2]
output <- summary(sc_nonpol.m); t <- coef(output)[ ,3]; df <- output$df 
pt(t, df, lower = FALSE)[2]
```

```{r, echo=FALSE}
sc_wt.out <- data.frame(emmeans::emmeans(sc_wt.m , "condition"))
sc_pol.out <- data.frame(emmeans::emmeans(sc_pol.m , "condition"))
sc_nonpol.out <-  data.frame(emmeans::emmeans(sc_nonpol.m , "condition"))

library(ggthemr)
ggthemr("solarized")
darken_swatch(amount = 0.3)

my.plot <- list(
  geom_bar(stat = "identity", width = 0.4, alpha = 0.5),
  geom_errorbar(
    aes(ymin = lower.CL, ymax = upper.CL, color = condition), width = 0.05
    ), 
  xlab(""),
  theme_bw(base_size = 11),
  theme(text = element_text(family = "Noto Serif"), 
        strip.text = element_text(size = 13),
        strip.background = element_rect(fill = "white"),
        legend.position = "none", panel.grid = element_blank(),
        axis.title.x = element_text(vjust = -1),
        axis.title.y = element_text(vjust = 2),
        axis.text.x = element_text(size = 13),
        axis.text.y = element_text(size = 11)),
  scale_y_continuous(limits = c(0.2, 0.6), oob = rescale_none))

sc_wt.out$title <- "Self-Censorship\n Around Weak Ties"
sc_wt.out %>%
  ggplot(aes(x = reorder(condition, desc(condition)), emmean, fill = condition)) +
  scale_x_discrete(
    labels = c("High\n Polarization\n Condition", "Low\n Polarization\n Condition")) +
  ylab("") +
  my.plot +
  facet_grid(. ~ title) -> f1

sc_pol.out$title <- "Self-Censorship\n on Political Topics"
sc_nonpol.out$title <- "Self-Censorship\n on Non-Political Topics"

sc_pol.out %>% 
  ggplot(aes(x = reorder(condition, desc(condition)), emmean, fill = condition)) +
  scale_x_discrete(
    labels = c("High\n Polarization\n Condition", "Low\n Polarization\n Condition")) +
  ylab("") +
  my.plot +
  facet_grid(. ~ title) -> f2

sc_nonpol.out %>%
  ggplot(aes(x = reorder(condition, desc(condition)), emmean, fill = condition)) +
  scale_x_discrete(
    labels = c("High\n Polarization\n Condition", "Low\n Polarization\n Condition")) +
  ylab("") +
  my.plot +
  facet_grid(. ~ title) -> f3

library(patchwork)
remove_y <- 
  theme(
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.title.y = element_blank()
    )

f1 + f2 + remove_y + f3 + remove_y -> out; out
```

*Figure 2*

```{r echo=FALSE}
minamount.m <- lm(xpd(minamount ~ condition + ..ctrl), dat)
minamount.int <- lm(xpd(minamount ~ condition*attextreme + ..ctrl), dat)
minamount.int2 <- lm(xpd(minamount ~ condition*strongp + ..ctrl), dat)

stargazer(minamount.m, minamount.int, minamount.int2, type="text")

minamount.out <-
  data.frame(
    emmeans::emmeans(minamount.int, ~condition|attextreme,
                     at = list(attextreme = c(0, 0.5 ,1)))
    )

my.plot2 <- 
  list(
    geom_line(), geom_ribbon(aes(ymin = lower.CL, ymax = upper.CL), alpha = 0.2),
    xlab("Attitude Extremity"), ylab(""),
    scale_x_continuous(breaks = c(0, 0.5, 1), labels = c("Moderate", "", "Extreme")),
    theme_bw(base_size=11),
    theme(text = element_text(family = "Noto Serif"),
          strip.text = element_text(size = 13),
          strip.background = element_rect(fill = "white"), 
          legend.position = "none", panel.grid=element_blank(), 
          axis.title.x = element_text(vjust = -1, size = 13),
          axis.title.y = element_text(vjust = 2),
          axis.text.x = element_text(size = 13),
          axis.text.y = element_text(size = 11),
          plot.title = element_text(size = 13)))

minamount.out$title <- "High Polarization Condition"
minamount.out[minamount.out$condition == "more", ] %>%
  ggplot(aes(attextreme, emmean, fill = condition)) +
  scale_y_continuous(limits = c(20, 60), oob = rescale_none) +
  my.plot2 +
  facet_grid(. ~ title) -> minamount1

minamount.out$title <- "Low Polarization Condition"
minamount.out[minamount.out$condition == "less", ] %>%
  ggplot(aes(attextreme, emmean, fill = condition)) +
  scale_y_continuous(limits = c(20, 60), oob = rescale_none) +
  my.plot2 +
  facet_grid(. ~ title) -> minamount2 

minamount1 + plot_spacer() + minamount2 + remove_y + plot_layout(widths = c(4,0.3,4,0.2)) + plot_annotation('Minimum Payment Requested for Public Political Discussion', theme = theme(text = element_text(family = "Noto Serif"))) -> minamount3; minamount3
```

*Figure 3*

```{r echo=FALSE}
email.m <- lm(xpd(email ~ condition + ..ctrl), dat)
email.int <- lm(xpd(email ~ condition*attextreme + ..ctrl), dat)
email.int2 <- lm(xpd(email ~ condition*strongp + ..ctrl), dat)

stargazer(email.m, email.int, email.int2, type="text")

email.out <- data.frame(
  emmeans::emmeans(email.int, ~condition|attextreme,
                   at = list(attextreme = c(0, 0.5 ,1)))
  )

email.out$title <- "High Polarization Condition"
email.out[email.out$condition == "more", ] %>%
  ggplot(aes(attextreme, emmean, fill = condition)) +
  scale_y_continuous(limits = c(0.1, 0.5), oob = rescale_none) +
  my.plot2 +
  facet_grid(. ~ title) -> email1

email.out$title <- "Low Polarization Condition"
email.out[email.out$condition == "less", ] %>%
  ggplot(aes(attextreme, emmean, fill = condition)) +
  scale_y_continuous(limits = c(0.1, 0.5), oob = rescale_none) +
  my.plot2 +
  facet_grid(. ~ title) -> email2

email1 + plot_spacer() + email2 + remove_y + plot_layout(widths = c(4,0.3,4,0.2)) + plot_annotation('Email Provided for Public Political Discussion', theme = theme(text = element_text(family = "Noto Serif"))) -> email3; email3
```

